In this code, we'll define a function to generate a random portfolio weights, perform an asset allocation, and then and analyze their returns.
Install Libs. (remove comments '#' if need to install the libraries)
# !pip install pandas
# !pip install pandas-datareader
# !pip install numpy
# !pip install plotly_express
# !pip install random
#import Libraries
import pandas as pd
from pandas_datareader import data as pdr
import numpy as np
import random
import plotly.graph_objects as go
functions already defined that we use on this code
# Define a function using Plotly Express
def plotly_data(df, title):
# Create figure
fig = go.Figure()
# Set title
fig.update_layout(title_text = title)
# For loop that plots all stock prices in the pandas dataframe df
for i in df.columns[0:]:
# Add range slider
#fig.update_layout(xaxis=dict(rangeselector = dict(buttons=list([dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=1, label="YTD", step="year", stepmode="todate"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all")])), rangeslider=dict( visible=True), type="date"))
# Add line graph
fig.add_scatter(x = df.index, y = df[i], name = i)
# Update Layout
fig.update_layout({'plot_bgcolor': "white"})
#fig.update_traces(line_width = 3)
fig.update_layout(legend=dict(orientation="h",))
fig.show()
# Define a function using Plotly Express, changes axis y to logarithm scale
def log_plotly_data(df, title):
# Create figure
fig = go.Figure()
# Set title
fig.update_layout(title_text = title)
# For loop that plots all stock prices in the pandas dataframe df
for i in df.columns[0:]:
# Add range slider
#fig.update_layout(xaxis=dict(rangeselector = dict(buttons=list([dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=1, label="YTD", step="year", stepmode="todate"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all")])), rangeslider=dict( visible=True), type="date"))
# Add line graph
fig.add_scatter(x = df.index, y = df[i], name = i)
# Update Layout
fig.update_layout({'plot_bgcolor': "white"})
#fig.update_traces(line_width = 3)
fig.update_layout(legend=dict(orientation="h",))
#changes y to logarithm scale
fig.update_yaxes(type="log")
fig.show()
# Define a function using Plotly Express, changes axis y to logarithm scale
def plotly_line(df, y, title):
# Create figure
fig = go.Figure()
fig.update_layout(title_text = title)
fig.add_scatter(x = df.index, y = y)
# Update Layout
fig.update_layout({'plot_bgcolor': "white"})
#fig.update_traces(line_width = 3)
fig.update_layout(legend=dict(orientation="h",))
#changes y to logarithm scale
fig.show()
# Define a function using Plotly Express, changes axis y to logarithm scale
def log_plotly_line(df, y, title):
# Create figure
fig = go.Figure()
fig.update_layout(title_text = title)
fig.add_scatter(x = df.index, y = y)
# Update Layout
fig.update_layout({'plot_bgcolor': "white"})
#fig.update_traces(line_width = 3)
fig.update_layout(legend=dict(orientation="h",))
#changes y to logarithm scale
fig.update_yaxes(type="log")
fig.show()
# Function to scale stock prices based on their initial starting price
# The objective of this function is to set all prices to start at a value of 1
def price_scaling(raw_prices_df):
scaled_prices_df = raw_prices_df.copy()
for i in raw_prices_df.columns[0:]:
scaled_prices_df[i] = raw_prices_df[i]/raw_prices_df[i][0]
return scaled_prices_df
file_name = input('Input the CSV file name: ')
initial_investment = int(input('Input the initial investment: '))
n_runs = int(input('Input the number of simulations: '))
## BRL5
# PETR4.SA VALE3.SA TAEE11.SA ITSA4.SA WEGE3.SA
## MAG7
# AAPL AMZN GOOG META MSFT NVDA TSLA
## BRL_top10
# CRFB3.SA CPLE6.SA ECOR3.SA ITUB4.SA JBSS3.SA RENT3.SA PRIO3.SA SBSP3.SA VALE3.SA VIVT3.SA
Input the CSV file name: BRL10 Input the initial investment: 1000000 Input the number of simulations: 5
#read CSV file
Stock_Prices_df = pd.read_csv(file_name)
#The code imports a DataFrame with num index [1,2,3...], this line replace the colum Date to Index
Stock_Prices_df.set_index(['Date'], inplace = True)
#obtain weights vector
n_assets = len(Stock_Prices_df.columns)
#lock vector to test function
weights = np.ones(n_assets) * 1/n_assets
weights
array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])
#Define a function that performs an Asset Allocation
def asset_allocation(df, initial_investment, weights):
''' Performs an asset Allocation for a given DF, initial investment value and weights'''
portfolio_df = df.copy()
# Scale stock prices using the "price_scaling"
scaled_df = price_scaling(df)
#enumerate method links Stocks tickers in columns along with a counter position weight (i), like an index
for i, stock in enumerate(scaled_df):
portfolio_df[stock] = weights[i] * scaled_df[stock] * initial_investment
# Sum up all values and place the result in a new column titled "portfolio value [$]"
portfolio_df['Total Value [$]'] = portfolio_df.sum(axis = 1, numeric_only = True)
# Calculate the portfolio percentage daily return and replace NaNs with zeros
portfolio_df['Daily Return [%]'] = portfolio_df['Total Value [$]'].pct_change(1) * 100
portfolio_df.replace(np.nan, 0, inplace = True)
return portfolio_df
#Asset Allocation with parameters defined
portfolio_df = asset_allocation(Stock_Prices_df, initial_investment, weights)
Eqw = portfolio_df
portfolio_df.round(2)
| CPLE6.SA | CRFB3.SA | ECOR3.SA | ITUB4.SA | JBSS3.SA | PRIO3.SA | RENT3.SA | SBSP3.SA | VALE3.SA | VIVT3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | ||||||||||||
| 2018-12-14 | 100000.00 | 100000.00 | 100000.00 | 100000.00 | 100000.00 | 100000.00 | 100000.00 | 100000.00 | 100000.00 | 100000.00 | 1000000.00 | 0.00 |
| 2018-12-17 | 99692.31 | 99555.55 | 97731.96 | 97325.43 | 98509.95 | 104978.13 | 97950.53 | 98694.52 | 100727.19 | 99056.60 | 994222.16 | -0.58 |
| 2018-12-18 | 99384.62 | 100833.31 | 97422.68 | 98904.25 | 98675.50 | 98269.33 | 97597.19 | 98074.41 | 101100.60 | 100856.98 | 991118.87 | -0.31 |
| 2018-12-19 | 96553.83 | 97333.32 | 96907.21 | 97580.61 | 95529.80 | 99183.28 | 98339.21 | 97225.86 | 98290.08 | 98575.67 | 975518.87 | -1.57 |
| 2018-12-20 | 96461.52 | 100333.34 | 97113.40 | 97552.45 | 94370.86 | 96246.96 | 99346.72 | 99869.46 | 97897.01 | 97278.95 | 976470.66 | 0.10 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 469772.65 | 69389.34 | 95695.89 | 108459.32 | 247430.61 | 2062226.53 | 244892.92 | 248979.82 | 209180.82 | 181340.74 | 3937368.64 | 1.12 |
| 2023-12-08 | 470264.01 | 67795.60 | 95904.60 | 109831.35 | 250366.22 | 2165775.45 | 243260.03 | 247953.70 | 209757.09 | 182260.56 | 4043168.61 | 2.69 |
| 2023-12-11 | 468298.44 | 67979.49 | 97574.33 | 109076.73 | 249108.11 | 2172095.38 | 243459.16 | 252681.14 | 209929.96 | 180250.58 | 4050453.32 | 0.18 |
| 2023-12-12 | 469772.65 | 69450.64 | 96426.39 | 108527.91 | 247430.61 | 2129314.58 | 241029.74 | 253707.28 | 210304.52 | 181783.62 | 4007747.96 | -1.05 |
| 2023-12-13 | 474195.16 | 70002.33 | 98096.11 | 110140.06 | 249632.31 | 2163830.82 | 245888.58 | 260377.04 | 209238.46 | 182839.70 | 4064240.57 | 1.41 |
1240 rows × 12 columns
#Plot data:
plotly_line(portfolio_df, portfolio_df['Total Value [$]'], "Portfolio Total Value")
log_plotly_line(portfolio_df, portfolio_df['Total Value [$]'], "Portfolio Total Value - Log Scale")
plotly_line(portfolio_df, portfolio_df['Daily Return [%]'], "Daily Return [%]")
plotly_data(portfolio_df, "Equal Weighted Porfolio")
log_plotly_data(portfolio_df, "Equal Weighted Porfolio")
def rand_weights(n):
''' Produces n random weights that sum to 1 '''
k = np.random.rand(n)
return k / sum(k)
#obtain weights vector
n_assets = len(Stock_Prices_df.columns)
weights = rand_weights(n_assets).round(4)
display(weights)
sum(weights)
array([0.1531, 0.1587, 0.1102, 0.128 , 0.1685, 0.0648, 0.0701, 0.0239,
0.0441, 0.0787])
1.0001
#Eqw -- Equal Weighted </p>
#Rdw_1 -- Random Weighted 1 </p>
#Rdw_2 -- Random Weighted 2 [...]
def random_port_generate(initial_investment, n_runs):
#obtain weights vector
n_assets = len(Stock_Prices_df.columns)
#lock vector to test function
eq_weights = np.ones(n_assets) * 1/n_assets
#Asset Allocation with parameters defined
Eqw_df = asset_allocation(Stock_Prices_df, initial_investment, eq_weights)[['Total Value [$]', 'Daily Return [%]']]
Eqw_df = Eqw_df.rename({'Total Value [$]':'Eqw [$]', 'Daily Return [%]':'Eqw [%]'}, axis="columns")
All_df = Eqw_df
# Placeholder to store all weights
weights_runs = np.zeros((n_runs, n_assets))
for i in range(n_runs):
# Generate random weights
weights = rand_weights(n_assets)
# Store the weights
weights_runs[i,:] = weights
# Random Asset Allocation
df = asset_allocation(Stock_Prices_df, initial_investment, weights)[['Total Value [$]', 'Daily Return [%]']]
#rename columns for iterate
Rdw = df.rename({'Total Value [$]':'Rdw_{}[$]'.format(i), 'Daily Return [%]':'Rdw_{}[%]'.format(i)}, axis="columns")
All_df = pd.merge(All_df, Rdw, on = 'Date')
#All_df = Eqw_df.join(Rdw)
print("Simulation Run = {}".format(i))
print("Weights = {}".format(weights_runs[i].round(3)))
print('\n')
return All_df
df = random_port_generate(initial_investment, n_runs)
daily_returns_df = df.iloc[:, 1::2]
total_values_df = df.iloc[:, 0::2]
display(df.round(2))
Simulation Run = 0 Weights = [0.156 0.062 0.091 0.123 0.072 0.061 0.106 0.174 0.066 0.088] Simulation Run = 1 Weights = [0.024 0.136 0.017 0.112 0.116 0.08 0.117 0.057 0.141 0.201] Simulation Run = 2 Weights = [0.078 0.105 0.131 0.147 0.065 0.114 0.091 0.101 0.024 0.144] Simulation Run = 3 Weights = [0.151 0.2 0.093 0.003 0.095 0.121 0.006 0.188 0.08 0.064] Simulation Run = 4 Weights = [0.21 0.115 0.15 0.022 0.213 0.109 0.136 0.002 0.001 0.043]
| Eqw [$] | Eqw [%] | Rdw_0[$] | Rdw_0[%] | Rdw_1[$] | Rdw_1[%] | Rdw_2[$] | Rdw_2[%] | Rdw_3[$] | Rdw_3[%] | Rdw_4[$] | Rdw_4[%] | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | ||||||||||||
| 2018-12-14 | 1000000.00 | 0.00 | 1000000.00 | 0.00 | 1000000.00 | 0.00 | 1000000.00 | 0.00 | 1000000.00 | 0.00 | 1000000.00 | 0.00 |
| 2018-12-17 | 994222.16 | -0.58 | 991063.47 | -0.89 | 994194.03 | -0.58 | 992720.18 | -0.73 | 998482.59 | -0.15 | 993906.60 | -0.61 |
| 2018-12-18 | 991118.87 | -0.31 | 989416.60 | -0.17 | 995783.35 | 0.16 | 989950.25 | -0.28 | 992621.07 | -0.59 | 987930.28 | -0.60 |
| 2018-12-19 | 975518.87 | -1.57 | 974476.14 | -1.51 | 977692.86 | -1.82 | 976290.79 | -1.38 | 973708.63 | -1.91 | 971199.50 | -1.69 |
| 2018-12-20 | 976470.66 | 0.10 | 977968.07 | 0.36 | 977568.14 | -0.01 | 977108.06 | 0.08 | 978980.54 | 0.54 | 969928.60 | -0.13 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 3937368.64 | 1.12 | 3430337.24 | 0.69 | 3366822.39 | 1.19 | 4019861.48 | 1.21 | 4437702.59 | 1.09 | 4427398.64 | 1.16 |
| 2023-12-08 | 4043168.61 | 2.69 | 3495232.75 | 1.89 | 3452606.75 | 2.55 | 4139779.89 | 2.98 | 4562664.05 | 2.82 | 4544646.04 | 2.65 |
| 2023-12-11 | 4050453.32 | 0.18 | 3502628.28 | 0.21 | 3454515.61 | 0.06 | 4147978.48 | 0.20 | 4575804.73 | 0.29 | 4546761.97 | 0.05 |
| 2023-12-12 | 4007747.96 | -1.05 | 3477492.10 | -0.72 | 3421319.83 | -0.96 | 4099627.08 | -1.17 | 4529579.15 | -1.01 | 4496813.88 | -1.10 |
| 2023-12-13 | 4064240.57 | 1.41 | 3527971.85 | 1.45 | 3465428.92 | 1.29 | 4161383.29 | 1.51 | 4595496.62 | 1.46 | 4559113.39 | 1.39 |
1240 rows × 12 columns
plotly_data(total_values_df, "Portfolios Total Value[$]")
log_plotly_data(total_values_df, "Portfolios Total Value[$]")
plotly_data(daily_returns_df, "Portfolio Daily Returns [%]")
import plotly.express as px
# Plot histograms for stocks daily returns using plotly express
fig = px.histogram(daily_returns_df)
fig.update_layout({'plot_bgcolor': "white"})
#Random Asset Allocation 01
weights_1 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_1))
Rdw_1 = asset_allocation(Stock_Prices_df, initial_investment, weights_1)
Rdw_1.round(2)
Value Invested: $ 1000000 Number of Stocks: 10 Weights: [0.1514 0.0687 0.0459 0.0808 0.0807 0.1477 0.1912 0.122 0.1059 0.0058]
| CPLE6.SA | CRFB3.SA | ECOR3.SA | ITUB4.SA | JBSS3.SA | PRIO3.SA | RENT3.SA | SBSP3.SA | VALE3.SA | VIVT3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | ||||||||||||
| 2018-12-14 | 151400.00 | 68700.00 | 45900.00 | 80800.00 | 80700.00 | 147700.00 | 191200.00 | 122000.00 | 105900.00 | 5800.00 | 1000100.00 | 0.00 |
| 2018-12-17 | 150934.16 | 68394.66 | 44858.97 | 78638.95 | 79497.53 | 155052.69 | 187281.42 | 120407.31 | 106670.09 | 5745.28 | 997481.07 | -0.26 |
| 2018-12-18 | 150468.31 | 69272.49 | 44717.01 | 79914.64 | 79631.13 | 145143.80 | 186605.82 | 119650.78 | 107065.54 | 5849.70 | 988319.21 | -0.92 |
| 2018-12-19 | 146182.50 | 66867.99 | 44480.41 | 78845.13 | 77092.55 | 146493.70 | 188024.58 | 118615.54 | 104089.19 | 5717.39 | 976408.99 | -1.21 |
| 2018-12-20 | 146042.75 | 68929.01 | 44575.05 | 78822.38 | 76157.28 | 142156.77 | 189950.93 | 121840.74 | 103672.93 | 5642.18 | 977790.00 | 0.14 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 711235.79 | 47670.48 | 43924.41 | 87635.13 | 199676.50 | 3045908.59 | 468235.26 | 303755.37 | 221522.49 | 10517.76 | 5140081.79 | 1.39 |
| 2023-12-08 | 711979.71 | 46575.58 | 44020.21 | 88743.73 | 202045.54 | 3198850.33 | 465113.18 | 302503.52 | 222132.76 | 10571.11 | 5292535.67 | 2.97 |
| 2023-12-11 | 709003.83 | 46701.91 | 44786.62 | 88133.99 | 201030.24 | 3208184.88 | 465493.92 | 308271.00 | 222315.83 | 10454.53 | 5304376.75 | 0.22 |
| 2023-12-12 | 711235.79 | 47712.59 | 44259.71 | 87690.55 | 199676.50 | 3144997.63 | 460848.87 | 309522.89 | 222712.49 | 10543.45 | 5239200.48 | -1.23 |
| 2023-12-13 | 717931.47 | 48091.60 | 45026.12 | 88993.17 | 201453.28 | 3195978.12 | 470138.96 | 317659.99 | 221583.53 | 10604.70 | 5317460.93 | 1.49 |
1240 rows × 12 columns
#Random Asset Allocation 02
weights_2 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_2))
Rdw_2 = asset_allocation(Stock_Prices_df, initial_investment, weights_2)
Rdw_2.round(2)
Value Invested: $ 1000000 Number of Stocks: 10 Weights: [0.2244 0.2137 0.148 0.0746 0.0101 0.116 0.018 0.0359 0.0021 0.1572]
| CPLE6.SA | CRFB3.SA | ECOR3.SA | ITUB4.SA | JBSS3.SA | PRIO3.SA | RENT3.SA | SBSP3.SA | VALE3.SA | VIVT3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | ||||||||||||
| 2018-12-14 | 224400.00 | 213700.00 | 148000.00 | 74600.00 | 10100.00 | 116000.00 | 18000.00 | 35900.00 | 2100.00 | 157200.00 | 1000000.00 | 0.00 |
| 2018-12-17 | 223709.54 | 212750.22 | 144643.30 | 72604.77 | 9949.51 | 121774.63 | 17631.10 | 35431.33 | 2115.27 | 155716.97 | 996326.63 | -0.37 |
| 2018-12-18 | 223019.08 | 215480.79 | 144185.57 | 73782.57 | 9966.23 | 113992.42 | 17567.49 | 35208.71 | 2123.11 | 158547.17 | 993873.15 | -0.25 |
| 2018-12-19 | 216666.80 | 208001.31 | 143422.67 | 72795.13 | 9648.51 | 115052.60 | 17701.06 | 34904.08 | 2064.09 | 154960.95 | 975217.21 | -1.88 |
| 2018-12-20 | 216459.66 | 214412.35 | 143727.83 | 72774.13 | 9531.46 | 111646.48 | 17882.41 | 35853.14 | 2055.84 | 152922.50 | 977265.79 | 0.21 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 1054169.83 | 148285.03 | 141629.92 | 80910.65 | 24990.49 | 2392182.78 | 44080.72 | 89383.75 | 4392.80 | 285067.64 | 4265093.62 | 1.16 |
| 2023-12-08 | 1055272.44 | 144879.20 | 141938.81 | 81934.19 | 25286.99 | 2512299.52 | 43786.81 | 89015.38 | 4404.90 | 286513.60 | 4385331.82 | 2.82 |
| 2023-12-11 | 1050861.69 | 145272.18 | 144410.01 | 81371.24 | 25159.92 | 2519630.64 | 43822.65 | 90712.53 | 4408.53 | 283353.92 | 4389003.30 | 0.08 |
| 2023-12-12 | 1054169.83 | 148416.03 | 142711.06 | 80961.82 | 24990.49 | 2470004.91 | 43385.35 | 91080.91 | 4416.39 | 285763.85 | 4345900.66 | -0.98 |
| 2023-12-13 | 1064093.93 | 149594.97 | 145182.25 | 82164.48 | 25212.86 | 2510043.75 | 44259.94 | 93475.36 | 4394.01 | 287424.01 | 4405845.57 | 1.38 |
1240 rows × 12 columns
#Random Asset Allocation 03
weights_3 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_3))
Rdw_3 = asset_allocation(Stock_Prices_df, initial_investment, weights_3)
Rdw_3.round(2)
Value Invested: $ 1000000 Number of Stocks: 10 Weights: [0.0405 0.0588 0.0667 0.0805 0.1221 0.006 0.2278 0.1794 0.1887 0.0295]
| CPLE6.SA | CRFB3.SA | ECOR3.SA | ITUB4.SA | JBSS3.SA | PRIO3.SA | RENT3.SA | SBSP3.SA | VALE3.SA | VIVT3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | ||||||||||||
| 2018-12-14 | 40500.00 | 58800.00 | 66700.00 | 80500.00 | 122100.00 | 6000.00 | 227800.00 | 179400.00 | 188700.00 | 29500.00 | 1000000.00 | 0.00 |
| 2018-12-17 | 40375.39 | 58538.67 | 65187.22 | 78346.97 | 120280.65 | 6298.69 | 223131.32 | 177057.96 | 190072.21 | 29221.70 | 988510.76 | -1.15 |
| 2018-12-18 | 40250.77 | 59289.99 | 64980.93 | 79617.92 | 120482.78 | 5896.16 | 222326.39 | 175945.49 | 190776.84 | 29752.81 | 989320.08 | 0.08 |
| 2018-12-19 | 39104.30 | 57231.99 | 64637.11 | 78552.39 | 116641.89 | 5951.00 | 224016.73 | 174423.19 | 185473.37 | 29079.82 | 975111.79 | -1.44 |
| 2018-12-20 | 39066.92 | 58996.00 | 64774.64 | 78529.72 | 115226.82 | 5774.82 | 226311.83 | 179165.81 | 184731.66 | 28697.29 | 981275.49 | 0.63 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 190257.92 | 40800.93 | 63829.16 | 87309.75 | 302112.78 | 123733.59 | 557866.06 | 446669.79 | 394724.21 | 53495.52 | 2260799.72 | -0.00 |
| 2023-12-08 | 190456.92 | 39863.81 | 63968.37 | 88414.24 | 305697.15 | 129946.53 | 554146.35 | 444828.94 | 395811.63 | 53766.87 | 2266900.81 | 0.27 |
| 2023-12-11 | 189660.87 | 39971.94 | 65082.08 | 87806.76 | 304161.00 | 130325.72 | 554599.97 | 453309.97 | 396137.84 | 53173.92 | 2274230.07 | 0.32 |
| 2023-12-12 | 190257.92 | 40836.98 | 64316.40 | 87364.97 | 302112.78 | 127758.87 | 549065.76 | 455150.87 | 396844.63 | 53626.17 | 2267335.35 | -0.30 |
| 2023-12-13 | 192049.04 | 41161.37 | 65430.11 | 88662.75 | 304801.06 | 129829.85 | 560134.18 | 467116.41 | 394832.97 | 53937.71 | 2297955.44 | 1.35 |
1240 rows × 12 columns
#Random Asset Allocation 04
weights_4 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_4))
Rdw_4 = asset_allocation(Stock_Prices_df, initial_investment, weights_4)
Rdw_4.round(2)
Value Invested: $ 1000000 Number of Stocks: 10 Weights: [0.0016 0.1769 0.1788 0.002 0.1117 0.0768 0.1183 0.076 0.1346 0.1233]
| CPLE6.SA | CRFB3.SA | ECOR3.SA | ITUB4.SA | JBSS3.SA | PRIO3.SA | RENT3.SA | SBSP3.SA | VALE3.SA | VIVT3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | ||||||||||||
| 2018-12-14 | 1600.00 | 176900.00 | 178800.00 | 2000.00 | 111700.00 | 76800.00 | 118300.00 | 76000.00 | 134600.00 | 123300.00 | 1000000.00 | 0.00 |
| 2018-12-17 | 1595.08 | 176113.77 | 174744.74 | 1946.51 | 110035.61 | 80623.20 | 115875.48 | 75007.83 | 135578.80 | 122136.79 | 993657.81 | -0.63 |
| 2018-12-18 | 1590.15 | 178374.13 | 174191.75 | 1978.09 | 110220.53 | 75470.84 | 115457.47 | 74536.55 | 136081.41 | 124356.66 | 992257.59 | -0.14 |
| 2018-12-19 | 1544.86 | 172182.65 | 173270.09 | 1951.61 | 106706.79 | 76172.76 | 116335.29 | 73891.65 | 132298.44 | 121543.80 | 975897.94 | -1.65 |
| 2018-12-20 | 1543.38 | 177489.68 | 173638.76 | 1951.05 | 105412.25 | 73917.67 | 117527.17 | 75900.79 | 131769.37 | 119944.94 | 979095.06 | 0.33 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 7516.36 | 122749.75 | 171104.26 | 2169.19 | 276379.99 | 1583789.98 | 289708.32 | 189224.66 | 281557.39 | 223593.13 | 3147793.02 | 0.90 |
| 2023-12-08 | 7524.22 | 119930.42 | 171477.43 | 2196.63 | 279659.06 | 1663315.54 | 287776.62 | 188444.81 | 282333.04 | 224727.27 | 3227385.05 | 2.53 |
| 2023-12-11 | 7492.77 | 120255.72 | 174462.91 | 2181.53 | 278253.75 | 1668169.25 | 288012.19 | 192037.67 | 282565.73 | 222248.97 | 3235680.50 | 0.26 |
| 2023-12-12 | 7516.36 | 122858.19 | 172410.39 | 2170.56 | 276379.99 | 1635313.60 | 285138.19 | 192817.54 | 283069.88 | 224139.20 | 3201813.90 | -1.05 |
| 2023-12-13 | 7587.12 | 123834.12 | 175395.85 | 2202.80 | 278839.30 | 1661822.07 | 290886.19 | 197886.55 | 281634.97 | 225441.35 | 3245530.31 | 1.37 |
1240 rows × 12 columns
#Random Asset Allocation 02
weights_5 = rand_weights(len(Stock_Prices_df.columns)).round(4)
print("Value Invested: $ {}".format(initial_investment))
print("Number of Stocks: {}".format(len(Stock_Prices_df.columns)))
print("Weights: {}".format(weights_5))
Rdw_5 = asset_allocation(Stock_Prices_df, initial_investment, weights_5)
Rdw_5.round(2)
Value Invested: $ 1000000 Number of Stocks: 10 Weights: [0.0852 0.0579 0.1741 0.1375 0.1459 0.0323 0.1367 0.0509 0.1212 0.0584]
| CPLE6.SA | CRFB3.SA | ECOR3.SA | ITUB4.SA | JBSS3.SA | PRIO3.SA | RENT3.SA | SBSP3.SA | VALE3.SA | VIVT3.SA | Total Value [$] | Daily Return [%] | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | ||||||||||||
| 2018-12-14 | 85200.00 | 57900.00 | 174100.00 | 137500.00 | 145900.00 | 32300.00 | 136700.00 | 50900.00 | 121200.00 | 58400.00 | 1000100.00 | 0.00 |
| 2018-12-17 | 84937.85 | 57642.67 | 170151.34 | 133822.46 | 143726.02 | 33907.93 | 133898.38 | 50235.51 | 122081.35 | 57849.05 | 988252.56 | -1.18 |
| 2018-12-18 | 84675.70 | 58382.49 | 169612.88 | 135993.35 | 143967.55 | 31740.99 | 133415.35 | 49919.88 | 122533.93 | 58900.48 | 989142.59 | 0.09 |
| 2018-12-19 | 82263.86 | 56355.99 | 168715.45 | 134173.33 | 139377.98 | 32036.20 | 134429.71 | 49487.96 | 119127.57 | 57568.19 | 973536.25 | -1.58 |
| 2018-12-20 | 82185.22 | 58093.00 | 169074.43 | 134134.61 | 137687.08 | 31087.77 | 135806.96 | 50833.55 | 118651.17 | 56810.90 | 974364.72 | 0.09 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2023-12-07 | 400246.30 | 40176.43 | 166606.55 | 149131.56 | 361001.26 | 666099.17 | 334768.62 | 126730.73 | 253527.16 | 105902.99 | 2604190.76 | 0.45 |
| 2023-12-08 | 400664.94 | 39253.65 | 166969.91 | 151018.11 | 365284.31 | 699545.47 | 332536.46 | 126208.44 | 254225.59 | 106440.17 | 2642147.05 | 1.46 |
| 2023-12-11 | 398990.27 | 39360.13 | 169876.91 | 149980.50 | 363448.73 | 701586.81 | 332808.67 | 128614.70 | 254435.11 | 105266.34 | 2644368.17 | 0.08 |
| 2023-12-12 | 400246.30 | 40211.92 | 167878.35 | 149225.88 | 361001.26 | 687768.61 | 329487.66 | 129137.01 | 254889.08 | 106161.63 | 2626007.70 | -0.69 |
| 2023-12-13 | 404014.27 | 40531.35 | 170785.33 | 151442.58 | 364213.55 | 698917.36 | 336129.69 | 132531.91 | 253597.01 | 106778.38 | 2658941.43 | 1.25 |
1240 rows × 12 columns
rand_value_df = pd.concat([ Eqw['Total Value [$]'],
Rdw_1['Total Value [$]'],
Rdw_2['Total Value [$]'],
Rdw_3['Total Value [$]'],
Rdw_4['Total Value [$]'],
Rdw_5['Total Value [$]']
], axis=1, join='inner').round(2)
#rand_value_df.to_csv('rand_value_df')
#read CSV file
rand_value_df = pd.read_csv('rand_value')
#The code imports a DataFrame with num index [1,2,3...], this line replace the colum Date to Index
rand_value_df.set_index(['Date'], inplace = True)
log_plotly_data(rand_value_df, "Total Value [$]")
rand_daily_df = pd.concat([ Eqw['Daily Return [%]'],
Rdw_1['Daily Return [%]'],
Rdw_2['Daily Return [%]'],
Rdw_3['Daily Return [%]'],
Rdw_4['Daily Return [%]'],
Rdw_5['Daily Return [%]']
], axis=1, join='inner').round(2)
#rand_daily_df.to_csv('rand_daily')
#read CSV file
rand_daily_df = pd.read_csv('rand_daily')
#The code imports a DataFrame with num index [1,2,3...], this line replace the colum Date to Index
rand_daily_df.set_index(['Date'], inplace = True)
plotly_data(rand_daily_df, "Total Value [$]")